<?php
/*-------------------------------------------------------------------------\
|
|  ========================================================
|  Fusion Registry GPL
|  Copyright (C) 2007 Fusion Scripts
|
|  This program is free software; you can redistribute it and/or
|  modify it under the terms of the GNU General Public License
|  as published by the Free Software Foundation; either version 2
|  of the License, or (at your option) any later version.
|
|  This program is distributed in the hope that it will be useful,
|  but WITHOUT ANY WARRANTY; without even the implied warranty of
|  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
|  GNU General Public License for more details.
|
|  You should have received a copy of the GNU General Public License
|  along with this program; if not, write to the Free Software
|  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
|  ========================================================
|
|  Email: info@fusionscripts.com
|  Web-Site: http://www.fusionscripts.com/
|
|  $Id: content.php 11 2008-08-03 05:45:23Z registry.fusion $
|
\-------------------------------------------------------------------------*/

/**
 * Functions for the main menu HTML of the Fusion Registry
 *
 * @todo Cache all statistics
 * @package Fusion Registry
 * @subpackage lib
 * @version $Id: content.php 11 2008-08-03 05:45:23Z registry.fusion $
 */

if ( ! defined( 'IN_IPB' ) )
{
    print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded all the relevant files.";
    exit();
}

/** Custom Fields class required by this class */
require_once( REGISTRY_PATH.'lib/custom_fields.php' );

/**
 * Class 'registry_content' contains functions for the main menu of the Fusion Registry.
 *
 * These functions construct the various statistic boxes on the main menu.
 * Other HTML and javascript bits should be placed in class {@link functions} if
 * they do not pertain to the main menu. This is to reduce the amount of
 * code included during run-time. <b>NOTE:</b> This class requires that a {@link custom_fields}
 * object be initialized, it will look by default in the parent class array
 * $lib['custom_fields'] for an existing object, if none found it will create one.
 *
 * @package Fusion Registry
 * @subpackage lib
 * @uses custom_fields
 */
class registry_content
{
    /**#@+ @var object */
    var $ipsclass;
    var $class  = "";
    /**#@-*/

    /**
     * Constructor, nothing too special here
     *
     * @param object $class Our parent class object
     * @return boolean TRUE
     */
    function auto_run($class)
    {
        $this->class = &$class;

        // Initialize custom_fields if it isn't already
        if ( !is_object( $this->class->lib['custom_fields'] ) )
        {
            $this->class->lib['custom_fields'] = new registry_custom_fields();
            $this->class->lib['custom_fields']->ipsclass =& $this->ipsclass;
            $this->class->lib['custom_fields']->auto_run(&$class);
        }

        return TRUE;
    }
    
    
    /**
     * Generate HTML for featured item
     *
     * @param integer $in_ipdl Pass in '1' to toggle the output template bit for the IPDL portal
     * @todo Change param $in_ipdl to be more generic to support more portals
     * @return string Template bit 'featured_item', or 'tmpl_ipdl_registry_featured_item' if {@link $in_ipdl} set to 1
     */
    function show_featureditem($in_ipdl='0')
    {
        $exists = 0;

        // If we are using random, go fetch!
        if ( $this->class->registry_config['featured_item_random'] == 1 )
        {
            if ( $this->class->registry_config['featured_item_only_image'] == 1 )
            {
                $q_extra = 'WHERE image_id IS NOT NULL';
            }

            $this->ipsclass->DB->query("SELECT id FROM ibf_registry_items $q_extra ORDER BY rand() LIMIT 1");

            $vehicle_data = $this->ipsclass->DB->fetch_row();

            $this->class->registry_config['featured_item_id'] = $vehicle_data['id'];

            $exists = 1;
        }
        else
        {
            // Make sure the configured item id exists
            $this->ipsclass->DB->query("SELECT id FROM ibf_registry_items WHERE id='". $this->class->registry_config['featured_item_id'] ."'");
            if ( $this->ipsclass->DB->get_num_rows() > 0 )
            {
                $exists = 1;
            }
        }

        if ( $exists )
        {
            // Get our fields that are going to be displayed
            $cf_highlight = $this->class->lib['custom_fields']->get_cf_highlight();
            
            // Create our sql id list, this will be used when retrieving the custom field entries.
            $cf_ids = "'";
            foreach ( $cf_highlight as $cf_num => $cf_row )
            {
                $cf_ids .= $cf_row['field_id']. "','";
            }
            $cf_ids = substr($cf_ids, 0, -2);

            // Grab the vehicle info and prep the HTML
            $this->ipsclass->DB->query("SELECT g.id, g.image_id, g.member_id, images.attach_id, images.attach_hits,
                                            images.attach_thumb_location, m.members_display_name, images.attach_is_image, images.attach_location
                                        FROM ibf_registry_items AS g 
                                            LEFT JOIN ibf_registry_images AS images ON images.attach_id = g.image_id
                                            LEFT JOIN ibf_members AS m ON g.member_id = m.id
                                        WHERE g.id='". $this->class->registry_config['featured_item_id'] ."'");

            $vehicle_data = $this->ipsclass->DB->fetch_row();

            // Do we have a highlight image?  If so, prep the HTML
            if ( (empty($vehicle_data['image_id']) == FALSE) AND ($vehicle_data['attach_is_image'] == 1) ) 
            {
                // Do we have a thumbnail?  If so, our job is simple here :)
                if ( (empty($vehicle_data['attach_thumb_location']) == FALSE) and
                     ($vehicle_data['attach_thumb_location'] != $vehicle_data['attach_location']) and
                     (@file_exists($this->ipsclass->vars['upload_dir']."/".$vehicle_data['attach_thumb_location'])) )
                {
                    // Form the correct image link if it's remote
                    $image_link = "{$this->ipsclass->vars['upload_url']}/{$vehicle_data['attach_thumb_location']}";

                    // Yippie, our thumbnail is already made for us :)
                    $vehicle_data['highlight_image'] = $this->ipsclass->compiled_templates['skin_registry_main']->featured_item_image_thumb(
                                                            array(  'filename' => $image_link, 
                                                                    'hits'     => $vehicle_data['attach_hits'],
                                                                    'type'     => 'garage',
                                                                    'id'       => $vehicle_data['id'],
                                                          )      );
                }
                else
                {
                    // Since we don't have a pre-made thumb we are going
                    //   to fall back on the old style :(

                    $img_size = array();

                    // Is this a remote image?
                    if ( ($vehicle_data['attach_thumb_location'] == "remote") OR preg_match("/^http:\/\//i", $vehicle_data['attach_location']) )
                    {
                        $img_size = @GetImageSize( $vehicle_data['attach_location'] );
                    }
                    else
                    {
                        $img_size = @GetImageSize( $this->ipsclass->vars['upload_url']."/".$vehicle_data['attach_location'] );
                    }
                
                    if ( $img_size[0] < 1 )
                    {
                        $img_size    = array();
                        $img_size[0] = $this->class->registry_config['thumbnail_resolution'] + 1;
                        $img_size[1] = $this->class->registry_config['thumbnail_resolution'] + 1;
                    }

                    // Form the correct image link if it's remote
                    $image_link = ( ($vehicle_data['attach_thumb_location'] == "remote") OR preg_match("/^http:\/\//i", $vehicle_data['attach_location']) )
                                ? $vehicle_data['attach_location'] 
                                : "{$this->ipsclass->vars['upload_url']}/{$vehicle_data['attach_location']}";

                    //----------------------------------------------------
                    // Do we need to scale?
                    //----------------------------------------------------
                    
                    if ( ( $img_size[0] <= $this->class->registry_config['thumbnail_resolution'] ) AND 
                         ( $img_size[1] <= $this->class->registry_config['thumbnail_resolution'] ) )
                    {
                        $vehicle_data['highlight_image'] = $in_ipdl
                                                         ? $this->ipsclass->compiled_templates['skin_registry_gallery']->gallery_item_thumb( array(  'filename' => $image_link, 
                                                                                                           'hits'     => $vehicle_data['attach_hits'],
                                                                                                           'type'     => 'garage',
                                                                                                           'id'       => $vehicle_data['id'],
                                                                                                 )      )

                                                         : $this->ipsclass->compiled_templates['skin_registry_gallery']->gallery_item_thumb( array(  'filename' => $image_link, 
                                                                                                                      'hits'     => $vehicle_data['attach_hits'],
                                                                                                                      'type'     => 'garage',
                                                                                                                      'id'       => $vehicle_data['id'],
                                                                                                            )      );
                    }
                    else
                    {
                        $im = $this->ipsclass->scale_image( array( 'max_width'  => $this->class->registry_config['thumbnail_resolution'],
                                                                  'max_height' => $this->class->registry_config['thumbnail_resolution'],
                                                                  'cur_width'  => $img_size[0],
                                                                  'cur_height' => $img_size[1]
                                                         )      );

                        $vehicle_data['highlight_image'] = $in_ipdl
                                                         ? $this->ipsclass->compiled_templates['skin_registry_main']->featured_item_image( array( 'filename' => $image_link, 
                                                                                                           'width'    => $im['img_width'],
                                                                                                           'height'   => $im['img_height'], 
                                                                                                           'hits'     => $vehicle_data['attach_hits'],
                                                                                                           'type'     => 'garage',
                                                                                                           'id'       => $vehicle_data['id'],
                                                                                                  )      )

                                                         : $this->ipsclass->compiled_templates['skin_registry_main']->featured_item_image( array( 'filename' => $image_link, 
                                                                                                                   'width'    => $im['img_width'],
                                                                                                                   'height'   => $im['img_height'], 
                                                                                                                   'hits'     => $vehicle_data['attach_hits'],
                                                                                                                   'type'     => 'garage',
                                                                                                                   'id'       => $vehicle_data['id'],
                                                                                                          )      );
                    }
                }
            }
            else
            {
                // No highlight image :(
                $vehicle_data['highlight_image'] = '&nbsp;<br /><br /><i><a href="'.$this->ipsclass->base_url.'&autocom=".REGISTRY_URL."&amp;cmd=view_item&amp;CID='.$vehicle_data['id'].'">'. $this->ipsclass->lang['no_image_available'] .'</a></i><br /><br />&nbsp;';
            }

            // Pass along the extra description if not present, set default
            $vehicle_data['featured_item_description'] = empty($this->class->registry_config['featured_item_description']) 
                                                       ? $this->ipsclass->lang['featured_item'] 
                                                       : $this->class->registry_config['featured_item_description'];

            // Get the  highlight field values
            $cf_entries = $this->class->lib['custom_fields']->get_cf_entries( array( 'WHERE' => "item_id='".$vehicle_data['id']."' AND field_id IN (".$cf_ids.")" ) );

            // Process the highlight field values
            $vehicle_data['highlight_fields'] = '';

            foreach ( $cf_highlight as $cf_num => $cf_row )
            {
                $vehicle_data['highlight_fields'] .= $this->class->lib['custom_fields']->process_cf_value( array( 'def' => $cf_row, 'value' => $cf_entries[ $cf_row['field_id'] ]['field_value'], 'type' => 'view' ) );

                $vehicle_data['highlight_fields'] .= ' ';
            }

            $vehicle_data['highlight_fields'] = substr($vehicle_data['highlight_fields'], 0, -1);

            return $in_ipdl
                 ? $this->ipsclass->compiled_templates['skin_registry_main']->tmpl_ipdl_registry_featured_item($vehicle_data)
                 : $this->ipsclass->compiled_templates['skin_registry_main']->featured_item($vehicle_data);
        }
        
        // Shouldn't ever get this far, but if the check if the vehicle exists fails, return a blank string
        return '';
    }


    /**
     * Generate HTML for latest commented vehicles
     *
     * @param integer $in_ipdl Pass in '1' to toggle the output template bit for the IPDL portal
     * @return string Template bit 'tmpl_lastcommented', or 'tmpl_ipdl_registry' if {@link $in_ipdl} set to 1.
     */
    function show_lastcommented($in_ipdl='0')
    {
        // Is it enabled?         
        if ( ! $this->class->registry_config['tmpl_lastcommented_on'] )
        {
             return '';
        }
        
        // Get our fields that are going to be displayed
        $cf_highlight = $this->class->lib['custom_fields']->get_cf_highlight();

        // Create our sql id list, this will be used when retrieving the custom field entries.
        $cf_ids = "'";
        foreach ( $cf_highlight as $cf_num => $cf_row )
        {
            $cf_ids .= $cf_row['field_id']. "','";
        }
        $cf_ids = substr($cf_ids, 0, -2);

        // What's the count? Default to 10
        $limit = $this->class->registry_config['tmpl_lastcommented_limit'] ? $this->class->registry_config['tmpl_lastcommented_limit'] : 10;
                  
        $html = "";
         
        $rs = $this->ipsclass->DB->query("SELECT gb.item_id AS id, gb.author_id AS member_id, gb.post_date AS POI, m.members_display_name 
                                          FROM ibf_registry_guestbooks AS gb 
                                             LEFT JOIN ibf_registry_items AS g ON gb.item_id = g.id
                                             LEFT JOIN ibf_members AS m ON gb.author_id = m.id
                                          ORDER BY POI DESC LIMIT $limit");
                     
        while ( $row = $this->ipsclass->DB->fetch_row($rs) )
        {
            // Get the  highlight field values
            $cf_entries = $this->class->lib['custom_fields']->get_cf_entries( array( 'WHERE' => "item_id='".$row['id']."' AND field_id IN (".$cf_ids.")" ) );

            // Process the highlight field values
            $row['highlight_fields'] = '';

            foreach ( $cf_highlight as $cf_num => $cf_row )
            {
                $row['highlight_fields'] .= $this->class->lib['custom_fields']->process_cf_value( array( 'def' => $cf_row, 'value' => $cf_entries[ $cf_row['field_id'] ]['field_value'], 'type' => 'view' ) );

                $row['highlight_fields'] .= ' ';
            }

            $row['highlight_fields'] = substr($row['highlight_fields'], 0, -1);

            // Change time to human readable format
            $row['POI'] = gmdate( $this->class->registry_config['date_format'], $row['POI'] + $this->ipsclass->get_time_offset() );

            // Guest check
            $row['members_display_name'] = ( $row['member_id'] == 0 ) ? 'Guest' : $row['members_display_name'];

            $html .= $in_ipdl
                   ? $this->ipsclass->compiled_templates['skin_registry_main']->tmpl_ipdl_registry_row($row)
                   : $this->ipsclass->compiled_templates['skin_registry_main']->tmpl_format_stat_row($row);
        }
         
        return $in_ipdl
             ? $this->ipsclass->compiled_templates['skin_registry_main']->tmpl_ipdl_registry($html,$this->ipsclass->lang['lastcommented'])
             : $this->ipsclass->compiled_templates['skin_registry_main']->tmpl_lastcommented($html);
    }


    /**
     * Generate HTML for latest updated items
     *
     * @param integer $in_ipdl Pass in '1' to toggle the output template bit for the IPDL portal
     * @return string Template bit 'tmpl_lastupdateditems', or 'tmpl_ipdl_registry' if {@link $in_ipdl} set to 1.
     */
    function show_lastupdateditems($in_ipdl='0')
    {
        // Is it enabled?         
        if ( ! $this->class->registry_config['tmpl_lastupdateditems_on'] )
        {
            return '';
        }

        // Get our fields that are going to be displayed
        $cf_highlight = $this->class->lib['custom_fields']->get_cf_highlight();

        // Create our sql id list, this will be used when retrieving the custom field entries.
        $cf_ids = "'";
        foreach ( $cf_highlight as $cf_num => $cf_row )
        {
            $cf_ids .= $cf_row['field_id']. "','";
        }
        $cf_ids = substr($cf_ids, 0, -2);

        // What's the count? Default to 10
        $limit = $this->class->registry_config['tmpl_lastupdateditems_limit'] ? $this->class->registry_config['tmpl_lastupdateditems_limit'] : 10;

        $html = "";

        $rs = $this->ipsclass->DB->query("SELECT g.id, g.member_id, g.date_updated AS POI, m.members_display_name 
                                          FROM ibf_registry_items AS g 
                                             LEFT JOIN ibf_members AS m ON g.member_id = m.id
                                          ORDER BY POI DESC LIMIT $limit");
                     
        while ( $row = $this->ipsclass->DB->fetch_row($rs) )
        {
            // Get the  highlight field values
            $cf_entries = $this->class->lib['custom_fields']->get_cf_entries( array( 'WHERE' => "item_id='".$row['id']."' AND field_id IN (".$cf_ids.")" ) );

            // Process the highlight field values
            $row['highlight_fields'] = '';

            foreach ( $cf_highlight as $cf_num => $cf_row )
            {
                $row['highlight_fields'] .= $this->class->lib['custom_fields']->process_cf_value( array( 'def' => $cf_row, 'value' => $cf_entries[ $cf_row['field_id'] ]['field_value'], 'type' => 'view' ) );

                $row['highlight_fields'] .= ' ';
            }

            $row['highlight_fields'] = substr($row['highlight_fields'], 0, -1);

            // Change time to human readable format
            $row['POI'] = gmdate( $this->class->registry_config['date_format'], $row['POI'] + $this->ipsclass->get_time_offset() );

            $html .= $in_ipdl
                   ? $this->ipsclass->compiled_templates['skin_registry_main']->tmpl_ipdl_registry_row($row)
                   : $this->ipsclass->compiled_templates['skin_registry_main']->tmpl_format_stat_row($row);
        }
         
        return $in_ipdl 
             ? $this->ipsclass->compiled_templates['skin_registry_main']->tmpl_ipdl_registry($html,$this->ipsclass->lang['lastupdateditems']) 
             : $this->ipsclass->compiled_templates['skin_registry_main']->tmpl_lastupdateditems($html);
    }


    /**
     * Generate HTML for latest updated modifications
     *
     * @param integer $in_ipdl Pass in '1' to toggle the output template bit for the IPDL portal
     * @return string Template bit 'tmpl_lastupdatedmods', or 'tmpl_ipdl_registry' if {@link $in_ipdl} set to 1.
     */
    function show_lastupdatedmods($in_ipdl='0')
    {
        // Is it enabled?         
        if ( ! $this->class->registry_config['tmpl_lastupdatedmods_on'] )
        {
            return '';
        }

        // What's the count? Default to 10
        $limit = $this->class->registry_config['tmpl_lastupdatedmods_limit'] ? $this->class->registry_config['tmpl_lastupdatedmods_limit'] : 10;

        $html = "";

        $rs = $this->ipsclass->DB->query("SELECT mods.id, mods.member_id, mods.title AS mod_title, mods.date_updated AS POI, m.members_display_name, mods.item_id 
                                          FROM ibf_registry_mods AS mods 
                                              LEFT JOIN ibf_members AS m ON mods.member_id = m.id
                                          ORDER BY POI DESC LIMIT $limit");
                     
        while ( $row = $this->ipsclass->DB->fetch_row($rs) )
        {
            // Change time to human readable format
            $row['POI'] = gmdate( $this->class->registry_config['date_format'], $row['POI'] + $this->ipsclass->get_time_offset() );

            $html .= $in_ipdl
                   ? $this->ipsclass->compiled_templates['skin_registry_main']->tmpl_ipdl_registry_mod_row($row)
                   : $this->ipsclass->compiled_templates['skin_registry_main']->tmpl_format_stat_mod_row($row);
        }
         
        return $in_ipdl
             ? $this->ipsclass->compiled_templates['skin_registry_main']->tmpl_ipdl_registry($html,$this->ipsclass->lang['lastupdatedmods'])
             : $this->ipsclass->compiled_templates['skin_registry_main']->tmpl_lastupdatedmods($html);
     }


    /**
     * Generate HTML for most modded items
     *
     * @param integer $in_ipdl Pass in '1' to toggle the output template bit for the IPDL portal
     * @return string Template bit 'tmpl_mostmodded', or 'tmpl_ipdl_registry' if {@link $in_ipdl} set to 1.
     */
    function show_mostmodded($in_ipdl='0')
    {
        // Is it enabled?         
        if ( ! $this->class->registry_config['tmpl_mostmodded_on'] )
        {
            return '';
        }

        // Get our fields that are going to be displayed
        $cf_highlight = $this->class->lib['custom_fields']->get_cf_highlight();

        // Create our sql id list, this will be used when retrieving the custom field entries.
        $cf_ids = "'";
        foreach ( $cf_highlight as $cf_num => $cf_row )
        {
            $cf_ids .= $cf_row['field_id']. "','";
        }
        $cf_ids = substr($cf_ids, 0, -2);

        // What's the count? Default to 10
        $limit = $this->class->registry_config['tmpl_mostmodded_limit'] ? $this->class->registry_config['tmpl_mostmodded_limit'] : 10;

        $html = "";
         
        $rs = $this->ipsclass->DB->query("SELECT g.id, g.member_id, COUNT(mods.id) AS POI, m.members_display_name 
                                          FROM ibf_registry_items AS g 
                                              LEFT JOIN ibf_members AS m ON g.member_id = m.id 
                                              LEFT JOIN ibf_registry_mods AS mods ON mods.item_id = g.id
                                          GROUP BY g.id 
                                          ORDER BY POI DESC LIMIT $limit");
                     
        while ( $row = $this->ipsclass->DB->fetch_row($rs) )
        {
            // Get the  highlight field values
            $cf_entries = $this->class->lib['custom_fields']->get_cf_entries( array( 'WHERE' => "item_id='".$row['id']."' AND field_id IN (".$cf_ids.")" ) );

            // Process the highlight field values
            $row['highlight_fields'] = '';

            foreach ( $cf_highlight as $cf_num => $cf_row )
            {
                $row['highlight_fields'] .= $this->class->lib['custom_fields']->process_cf_value( array( 'def' => $cf_row, 'value' => $cf_entries[ $cf_row['field_id'] ]['field_value'], 'type' => 'view' ) );

                $row['highlight_fields'] .= ' ';
            }

            $row['highlight_fields'] = substr($row['highlight_fields'], 0, -1);

            $html .= $in_ipdl
                   ? $this->ipsclass->compiled_templates['skin_registry_main']->tmpl_ipdl_registry_row($row)
                   : $this->ipsclass->compiled_templates['skin_registry_main']->tmpl_format_stat_row($row);
        }
         
        return $in_ipdl
             ? $this->ipsclass->compiled_templates['skin_registry_main']->tmpl_ipdl_registry($html,$this->ipsclass->lang['mostmodded'])
             : $this->ipsclass->compiled_templates['skin_registry_main']->tmpl_mostmodded($html);
    }


    /**
     * Generate HTML for most money spent
     *
     * @param integer $in_ipdl Pass in '1' to toggle the output template bit for the IPDL portal
     * @todo Append currency from custom fields if present maybe? Not sure how to handle this yet...
     * @return string Template bit 'tmpl_mostmoneyspent', or 'tmpl_ipdl_registry' if {@link $in_ipdl} set to 1.
     */
    function show_mostmoneyspent($in_ipdl='0')
    {
        // Is it enabled?         
        if ( ! $this->class->registry_config['tmpl_mostmoneyspent_on'] )
        {
            return '';
        }

        // Get our fields that are going to be displayed
        $cf_highlight = $this->class->lib['custom_fields']->get_cf_highlight();

        // Create our sql id list, this will be used when retrieving the custom field entries.
        $cf_ids = "'";
        foreach ( $cf_highlight as $cf_num => $cf_row )
        {
            $cf_ids .= $cf_row['field_id']. "','";
        }
        $cf_ids = substr($cf_ids, 0, -2);

        // What's the count? Default to 10
        $limit = $this->class->registry_config['tmpl_mostmoneyspent_limit'] ? $this->class->registry_config['tmpl_mostmoneyspent_limit'] : 10;
                  
        $html = "";
         
        $rs = $this->ipsclass->DB->query("SELECT g.id, g.member_id, (SUM(mods.install_price) + SUM(mods.price)) AS POI, m.members_display_name 
                                          FROM ibf_registry_items AS g 
                                            LEFT JOIN ibf_registry_mods AS mods ON mods.item_id = g.id 
                                            LEFT JOIN ibf_members AS m ON g.member_id = m.id
                                          GROUP BY g.id 
                                          ORDER BY POI DESC LIMIT $limit");
                     
        while ( $row = $this->ipsclass->DB->fetch_row($rs) )
        {
            // Get the  highlight field values
            $cf_entries = $this->class->lib['custom_fields']->get_cf_entries( array( 'WHERE' => "item_id='".$row['id']."' AND field_id IN (".$cf_ids.")" ) );

            // Process the highlight field values
            $row['highlight_fields'] = '';

            foreach ( $cf_highlight as $cf_num => $cf_row )
            {
                $row['highlight_fields'] .= $this->class->lib['custom_fields']->process_cf_value( array( 'def' => $cf_row, 'value' => $cf_entries[ $cf_row['field_id'] ]['field_value'], 'type' => 'view' ) );

                $row['highlight_fields'] .= ' ';
            }

            $row['highlight_fields'] = substr($row['highlight_fields'], 0, -1);

            // Append currency
            //$row['POI'] .= ' '. $row['currency'];

            $html .= $in_ipdl
                   ? $this->ipsclass->compiled_templates['skin_registry_main']->tmpl_ipdl_registry_row($row)
                   : $this->ipsclass->compiled_templates['skin_registry_main']->tmpl_format_stat_row($row);
        }
         
        return $in_ipdl
             ? $this->ipsclass->compiled_templates['skin_registry_main']->tmpl_ipdl_registry($html,$this->ipsclass->lang['mostmoneyspent'])
             : $this->ipsclass->compiled_templates['skin_registry_main']->tmpl_mostmoneyspent($html);
    }


    /**
     * Generate HTML for most viewed items
     *
     * @param integer $in_ipdl Pass in '1' to toggle the output template bit for the IPDL portal
     * @return string Template bit 'tmpl_mostviewed', or 'tmpl_ipdl_registry' if {@link $in_ipdl} set to 1.
     */
    function show_mostviewed($in_ipdl='0')
    {
        // Is it enabled?         
        if ( ! $this->class->registry_config['tmpl_mostviewed_on'] )
        {
            return '';
        }

        // Get our fields that are going to be displayed
        $cf_highlight = $this->class->lib['custom_fields']->get_cf_highlight();

        // Create our sql id list, this will be used when retrieving the custom field entries.
        $cf_ids = "'";
        foreach ( $cf_highlight as $cf_num => $cf_row )
        {
            $cf_ids .= $cf_row['field_id']. "','";
        }
        $cf_ids = substr($cf_ids, 0, -2);

        // What's the count? Default to 10
        $limit = $this->class->registry_config['tmpl_mostviewed_limit'] ? $this->class->registry_config['tmpl_mostviewed_limit'] : 10;
                  
        $html = "";
         
        $rs = $this->ipsclass->DB->query("SELECT g.id, g.member_id, g.views AS POI, m.members_display_name 
                                          FROM ibf_registry_items AS g 
                                            LEFT JOIN ibf_members AS m ON g.member_id = m.id
                                          ORDER BY POI DESC LIMIT $limit");
                     
        while ( $row = $this->ipsclass->DB->fetch_row($rs) )
        {
            // Get the  highlight field values
            $cf_entries = $this->class->lib['custom_fields']->get_cf_entries( array( 'WHERE' => "item_id='".$row['id']."' AND field_id IN (".$cf_ids.")" ) );

            // Process the highlight field values
            $row['highlight_fields'] = '';

            foreach ( $cf_highlight as $cf_num => $cf_row )
            {
                $row['highlight_fields'] .= $this->class->lib['custom_fields']->process_cf_value( array( 'def' => $cf_row, 'value' => $cf_entries[ $cf_row['field_id'] ]['field_value'], 'type' => 'view' ) );

                $row['highlight_fields'] .= ' ';
            }

            $row['highlight_fields'] = substr($row['highlight_fields'], 0, -1);

            $html .= $in_ipdl
                   ? $this->ipsclass->compiled_templates['skin_registry_main']->tmpl_ipdl_registry_row($row)
                   : $this->ipsclass->compiled_templates['skin_registry_main']->tmpl_format_stat_row($row);
        }
         
        return $in_ipdl
             ? $this->ipsclass->compiled_templates['skin_registry_main']->tmpl_ipdl_registry($html,$this->ipsclass->lang['mostviewed'])
             : $this->ipsclass->compiled_templates['skin_registry_main']->tmpl_mostviewed($html);
    }


    /**
     * Generate HTML for newest items
     *
     * @param integer $in_ipdl Pass in '1' to toggle the output template bit for the IPDL portal
     * @return string Template bit 'tmpl_newestitems', or 'tmpl_ipdl_registry' if {@link $in_ipdl} set to 1.
     */
    function show_newestitems($in_ipdl='0')
    {
        // Is it enabled?         
        if ( ! $this->class->registry_config['tmpl_newestitems_on'] )
        {
            return '';
        }

        // Get our fields that are going to be displayed
        $cf_highlight = $this->class->lib['custom_fields']->get_cf_highlight();

        // Create our sql id list, this will be used when retrieving the custom field entries.
        $cf_ids = "'";
        foreach ( $cf_highlight as $cf_num => $cf_row )
        {
            $cf_ids .= $cf_row['field_id']. "','";
        }
        $cf_ids = substr($cf_ids, 0, -2);

        // What's the count? Default to 10
        $limit = $this->class->registry_config['tmpl_newestitems_limit'] ? $this->class->registry_config['tmpl_newestitems_limit'] : 10;
                  
        $html = "";
         
        $rs = $this->ipsclass->DB->query("SELECT g.id, g.member_id, g.date_created AS POI, m.members_display_name 
                                          FROM ibf_registry_items AS g 
                                              LEFT JOIN ibf_members AS m ON g.member_id = m.id
                                          ORDER BY POI DESC LIMIT $limit");
                     
        while ( $row = $this->ipsclass->DB->fetch_row($rs) )
        {
            // Get the  highlight field values
            $cf_entries = $this->class->lib['custom_fields']->get_cf_entries( array( 'WHERE' => "item_id='".$row['id']."' AND field_id IN (".$cf_ids.")" ) );

            // Process the highlight field values
            $row['highlight_fields'] = '';

            foreach ( $cf_highlight as $cf_num => $cf_row )
            {
                $row['highlight_fields'] .= $this->class->lib['custom_fields']->process_cf_value( array( 'def' => $cf_row, 'value' => $cf_entries[ $cf_row['field_id'] ]['field_value'], 'type' => 'view' ) );

                $row['highlight_fields'] .= ' ';
            }

            $row['highlight_fields'] = substr($row['highlight_fields'], 0, -1);

            // Change time to human readable format
            $row['POI'] = gmdate( $this->class->registry_config['date_format'], $row['POI'] + $this->ipsclass->get_time_offset() );

            $html .= $in_ipdl
                   ? $this->ipsclass->compiled_templates['skin_registry_main']->tmpl_ipdl_registry_row($row)
                   : $this->ipsclass->compiled_templates['skin_registry_main']->tmpl_format_stat_row($row);
        }
         
        return $in_ipdl
             ? $this->ipsclass->compiled_templates['skin_registry_main']->tmpl_ipdl_registry($html, $this->ipsclass->lang['newestitems'])
             : $this->ipsclass->compiled_templates['skin_registry_main']->tmpl_newestitems($html);
    }


    /**
     * Generate HTML for newest modifications
     *
     * @param integer $in_ipdl Pass in '1' to toggle the output template bit for the IPDL portal
     * @return string Template bit 'tmpl_newestmods', or 'tmpl_ipdl_registry' if {@link $in_ipdl} set to 1.
     */
    function show_newestmods($in_ipdl='0')
    {
        // Is it enabled?         
        if ( ! $this->class->registry_config['tmpl_newestmods_on'] )
        {
            return '';
        }

        // What's the count? Default to 10
        $limit = $this->class->registry_config['tmpl_newestmods_limit'] ? $this->class->registry_config['tmpl_newestmods_limit'] : 10;
                  
        $html = "";
         
        $rs = $this->ipsclass->DB->query("SELECT mods.id, mods.member_id, mods.title AS mod_title, mods.date_created AS POI, m.members_display_name, mods.item_id 
                                          FROM ibf_registry_mods AS mods 
                                            LEFT JOIN ibf_members AS m ON mods.member_id = m.id
                                          ORDER BY POI DESC LIMIT $limit");
                     
        while ( $row = $this->ipsclass->DB->fetch_row($rs) )
        {
            // Change time to human readable format
            $row['POI'] = gmdate( $this->class->registry_config['date_format'], $row['POI'] + $this->ipsclass->get_time_offset() );

            $html .= $in_ipdl
                   ? $this->ipsclass->compiled_templates['skin_registry_main']->tmpl_ipdl_registry_mod_row($row)
                   : $this->ipsclass->compiled_templates['skin_registry_main']->tmpl_format_stat_mod_row($row);
        }
         
        return $in_ipdl
             ? $this->ipsclass->compiled_templates['skin_registry_main']->tmpl_ipdl_registry($html,$this->ipsclass->lang['newestmods'])
             : $this->ipsclass->compiled_templates['skin_registry_main']->tmpl_newestmods($html);
    }
}

?>